package com.bdqn.bean.bo;

/**
 * @author 忍
 * @title: RestResponseBo
 * @projectName Teaching
 * @description: TODO
 * @date 2019/6/2110:27
 */
public class RestResponseBo<T> {

    /**
     * 请求是否成功
     */
    private boolean success;

    /**
     * 错误信息
     */
    private String message;

    /**
     * 状态码: 同 http 请求状态码
     */
    private int code;

    /**
     * 服务器响应时间戳(unix)
     */
    private long timestamp;

    /**
     * 请求返回数据
     */
    private T payload;

    public RestResponseBo() {
        this.timestamp = System.currentTimeMillis() / 1000;
    }

    public RestResponseBo(boolean success, int code, String message, T payload) {
        this.timestamp = System.currentTimeMillis() / 1000;
        this.success = success;
        this.code = code;
        this.message = message;
        this.payload = payload;
    }

    /**
     * 请求成功生成回复对象
     */
    public static<T> RestResponseBo ok(int code, T payload) {
        return new RestResponseBo(true, code, "ok", payload);
    }

    public static<T> RestResponseBo ok(T payload) {
        return ok(200, payload);
    }

    public static<T> RestResponseBo ok(int code) {
        return ok(code, null);
    }

    public static<T> RestResponseBo ok() {
        return ok(200, null);
    }

    /**
     * 请求失败生成回复对象
     */
    public static<T> RestResponseBo fail(int code, String message) {
        return new RestResponseBo(false, code, "OK",null);
    }

    public static<T> RestResponseBo fail(String message) {
        return fail(400, message);
    }

    public static<T> RestResponseBo fail(int code) {
        return fail(code, "fail");
    }
    public static<T> RestResponseBo fail() {
        return fail(400, "fail");
    }


    /**
     * gett 方法
     */
    public T getPayload() {
        return payload;
    }

    public void setPayload(T payload) {
        this.payload = payload;
    }

    public boolean isSuccess() {
        return success;
    }

    public void setSuccess(boolean success) {
        this.success = success;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public long getTimestamp() {
        return timestamp;
    }
}

